package com.welike.shibo.controller.v2.api;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.welike.shibo.aspect.annotation.AutoLog;
import com.welike.shibo.eneity.JsonResult;
import com.welike.shibo.service.UserService;
import com.welike.shibo.utils.ResultUtils;
import com.welike.shibo.vo.UserInfoListVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author fangyu
 * @version v1.0.0
 * @since 2019/11/20 5:32 下午
 */
@Slf4j
@Api(value = "会员相关接口")
@RestController
@RequestMapping("/v2/api/member")
public class MemberController {

    @Resource
    private UserService userService;

    @AutoLog(operateState = "会员列表")
    @RequiresPermissions("member:list")
    @GetMapping("/list")
    public JsonResult list(
            @ApiParam(name = "page", value = "页数", required = false)
            @RequestParam(value = "page", defaultValue = "1", required = false) int page,
            @ApiParam(name = "pageSize", value = "每页条数", required = false)
            @RequestParam(value = "pageSize", defaultValue = "10", required = false)int pageSize,
            @ApiParam(name = "name", value = "搜索关键字", required = false)
            @RequestParam(value = "name")String key
    ){
        IPage<UserInfoListVO> iPage = userService.queryAllUserInfo(page, pageSize, key);
        return ResultUtils.success("list", iPage);
    }

    /**
     * 上传头像
     * @param avatar 头像
     * @return JsonResult
     * @throws IOException IO异常
     */
    @AutoLog(operateState = "上传头像")
    @RequiresPermissions("member:update")
    @PostMapping(value = "/avatar/upload")
    public JsonResult upload(@RequestParam("file") MultipartFile avatar) throws IOException {
        String avatarUrl = userService.uploadAvatar(avatar);
        Map<String, Object> result = new HashMap<>();
        result.put("avatar", avatarUrl);
        return ResultUtils.success(result);
    }

    @AutoLog(operateState = "更新会员信息")
    @RequiresPermissions("member:update")
    @PostMapping(value = "/updateMember")
    public JsonResult updateMember(@RequestBody JSONObject requestJson){
        return userService.updateUser(requestJson);
    }

    @AutoLog(operateState = "更新会员状态")
    @RequiresPermissions("member:update")
    @PostMapping(value = "/status")
    public JsonResult updateStatus(@RequestBody JSONObject requestJson){
        return userService.updateStatus(requestJson);
    }


}
